package com.hg.onlineshop.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hg.onlineshop.entity.Address;
import com.hg.onlineshop.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("/save")
    public String saveAddress(@RequestBody Address address) {
        Integer id = address.getId();
        if (id == null) {
            // 新增收货地址
            address.setDelStatus("0");
            addressMapper.insert(address);
        } else {
            // 修改收货地址
            addressMapper.updateById(address);
        }
        return "S";
    }

    @GetMapping("/list/{memberId}")
    public List<Address> findAddressByMemberId(@PathVariable("memberId")Integer memberId) {
        List<Address> addresses = null;
        if (memberId == null) {
            return addresses;
        }
        addresses = addressMapper.selectList(Wrappers.<Address>lambdaQuery().eq(Address::getMemberId, memberId).eq(Address::getDelStatus, "0"));
        return addresses;
    }
}
